﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Top.Redis.Cache
{
    /// <summary>
    /// 缓存接口
    /// </summary>
    public interface ICache
    {
        /// <summary> 缓存的实例名称
        /// </summary>
        string Name { get; }

        /// <summary> 设置缓存项
        /// </summary>
        /// <param name="key">缓存项名称</param>
        /// <param name="value">缓存值</param>
        /// <param name="slidingExpiredTime">指定时间间隔内不访问缓存即过期</param>
        void Set(string key, object value, TimeSpan slidingExpiredTime);

        /// <summary> 设置缓存项
        /// </summary>
        /// <param name="key">缓存项名称</param>
        /// <param name="value">缓存值</param>
        /// <param name="absoluteExpiredTime">指定时间点过期,无论期间是否访问缓存项</param>
        void Set(string key, object value, DateTime absoluteExpiredTime);

        /// <summary> 设置缓存项
        /// </summary>
        /// <param name="key">缓存项名称</param>
        /// <param name="value">缓存值</param>
        /// <param name="slidingExpiredTime">指定时间间隔内不访问缓存即过期</param>
        /// <param name="absoluteExpiredTime">指定时间点过期,无论期间是否访问缓存项</param>
        void Set(string key, object value, TimeSpan slidingExpiredTime, DateTime absoluteExpiredTime);

        /// <summary> 新增缓存项,如果key已经存在,返回false
        /// </summary>
        /// <param name="key">缓存项名称</param>
        /// <param name="value">缓存值</param>
        /// <param name="slidingExpiredTime">指定时间间隔内不访问缓存即过期</param>
        bool Add(string key, object value, TimeSpan slidingExpiredTime);

        /// <summary> 新增缓存项,如果key已经存在,返回false
        /// </summary>
        /// <param name="key">缓存项名称</param>
        /// <param name="value">缓存值</param>
        /// <param name="absoluteExpiredTime">指定时间点过期,无论期间是否访问缓存项</param>
        bool Add(string key, object value, DateTime absoluteExpiredTime);

        /// <summary> 新增缓存项,如果key已经存在,返回false
        /// </summary>
        /// <param name="key">缓存项名称</param>
        /// <param name="value">缓存值</param>
        /// <param name="slidingExpiredTime">指定时间间隔内不访问缓存即过期</param>
        /// <param name="absoluteExpiredTime">指定时间点过期,无论期间是否访问缓存项</param>
        bool Add(string key, object value, TimeSpan slidingExpiredTime, DateTime absoluteExpiredTime);

        /// <summary> 替换缓存值,如果key不存在返回false
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="newValue">新的缓存值</param>
        bool Replace(string key, object value);

        /// <summary> 替换缓存值,如果key不存在,或oldvalue不正确,返回false
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="oldValue">旧的缓存值</param>
        /// <param name="newValue">新的缓存值</param>
        bool Replace(string key, object oldValue, object newValue);


        /// <summary> 检查是否包含某个Key的缓存,该操作不应该滑动缓存的过期时间
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <returns></returns>
        bool ContiansKey(string key);

        /// <summary> 检查是否包含某个Key的缓存,并指定是否滑动缓存的过期时间
        /// </summary>
        /// <param name="key">缓存key</param>
        /// <param name="slidingExpiration">是否根据缓存的初始设定,滑动缓存的过期时间</param>
        bool ContiansKey(string key, bool slidingExpiration);

        /// <summary> 根据缓存键值获取缓存,并滑动缓存的过期时间,不存在返回null
        /// </summary>
        /// <param name="key">缓存的键</param>
        object this[string key] { get; }

        /// <summary> 根据缓存键值获取缓存,不存在返回null
        /// </summary>
        /// <param name="key">缓存的键</param>
        /// <param name="slidingExpiration">是否根据缓存的初始设定,滑动缓存的过期时间 </param>
        object this[string key, bool slidingExpiration] { get; }

        /// <summary> 批量获取缓存,并滑动缓存的过期时间
        /// </summary>
        /// <param name="keys">缓存键的集合</param>
        IDictionary<string, object> GetValues(params string[] keys);

        /// <summary> 批量获取缓存,并指定是否滑动缓存的过期时间
        /// </summary>
        /// <param name="keys">缓存键的集合</param>
        /// <param name="slidingExpiration">是否根据缓存的初始设定,滑动缓存的过期时间 </param>
        IDictionary<string, object> GetValues(IEnumerable<string> keys, bool slidingExpiration);
    }
}
